'''
1.使用itsdangerous  实现token方法
2.使用pyjwt  实现token方法
'''

# 使用itsdangerous
# 1.导入itsdangerous
from datetime import datetime, timedelta
from flask import current_app
from itsdangerous import TimedJSONWebSignatureSerializer as Ser
# 2.定义生成token的方法


# 使用项目自定义的jwt生成token方法
from common.token.jwt_auth import generic_pyjwt_token
def generic_login_token(user_id):
    # 业务逻辑
    # 创建序列化器对象
    # s = Ser(secret_key=current_app.config.get('SECRET_KEY'),expires_in=current_app.config.get('TOKEN_EXPIRES'))
    # # 组织数据
    # token = s.dumps({
    #     'id':user_id
    # })
    # # 3.返回
    # return token.decode()
    payload = {
        'user_id':user_id,
        'is_refresh':False

    }
    # expiry 为token的过期时间 需要设置一个时间戳
    now = datetime.now()
    expiry = now + timedelta(hours=current_app.config.get('TOKEN_EXPIRES_HOURS'))
    token = generic_pyjwt_token(
        payload=payload,
        expiry=expiry
    )
    payload = {
        'user_id':user_id,
        'is_refresh':True
    }
    expiry = now + timedelta(days=current_app.config.get('TOKEN_EXPIRES_DAYS'))
    refresh_token = generic_pyjwt_token(
        payload=payload,
        expiry=expiry
    )
    return token,refresh_token